<?php

namespace Home\Lib;

use Think\Controller;

class DataEntry extends Controller
{
    // 土建专业数据录入页面
    public function tujian()
    {
        $module = M('module');
        $baseInfo = M('base_info');
        $baseData = $baseInfo->where(array('batch' => session('batch'), 'project_code' => I('get.project_code')))->find();
        $result = $module->where(array('type' => I('get.type')))->group('number')->select();
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->assign('Tjdata', $result);
        $this->assign('baseData', $baseData);
        $this->assign('project_code', I('get.project_code'));
        $this->assign('major', I('get.type'));
        $this->display('tujian');
    }

    // 铁塔专业数据录入页面
    public function tieta()
    {
        $module = M('module');
        $baseInfo = M('base_info');
        $baseData = $baseInfo->where(array('batch' => session('batch'), 'project_code' => I('get.project_code')))->find();
        $result = $module->where(array('type' => I('get.type')))->group('number')->select();
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->assign('Ttdata', $result);
        $this->assign('baseData', $baseData);
        $this->assign('project_code', I('get.project_code'));
        $this->assign('major', I('get.type'));
        $this->display('tieta');
    }

    // 配套专业数据录入页面
    public function peitao()
    {
        $module = M('module');
        $baseInfo = M('base_info');
        $baseData = $baseInfo->where(array('batch' => session('batch'), 'project_code' => I('get.project_code')))->find();
        $result = $module->where(array('type' => I('get.type')))->group('number')->select();
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->assign('Ptdata', $result);
        $this->assign('baseData', $baseData);
        $this->assign('project_code', I('get.project_code'));
        $this->assign('major', I('get.type'));
        $this->display('peitao');
    }

    // 交流专业数据录入页面
    public function jiaoliu()
    {
        // number为市电引入类型,
        $module = M('module');
        $baseInfo = M('base_info');
        $baseData = $baseInfo->where(array('batch' => session('batch'), 'project_code' => I('get.project_code')))->find();
        $result = $module->where(array('type' => I('get.type')))->group('number')->select();
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->assign('JLdata', $result);
        $this->assign('baseData', $baseData);
        $this->assign('project_code', I('get.project_code'));
        $this->assign('major', I('get.type'));
        $this->display('jiaoliu');
    }

      // 提交申报的表单
    public function submit()
    {
        $logdata = M('logdata');
        $map = array(
        'uid' => session(C('USER_AUTH_KEY')),
        'batch' => session('batch'),
        'project_code' => I('post.project_code'),
        'type' => I('post.major'),
        );
        if ($logdata->where($map)->find()) {
            $this->error('已经申报过了', U('Home/Module/baseInfo', array('basetype' => I('post.major'))));
        } else {
            $filepath = '';
            if ($_FILES['fileUpload']['name']) {
                $filepath = $this->up($_FILES['fileUpload']);
            }
            $data = array(
            'workload' => I('workload'),
            'ecost' => I('post.ecost'),
            'ocost' => I('post.ocost'),
            'diff' => I('post.diff'),
            'diff_reason' => I('post.diff_reason'),
            'finish' => I('post.finish'),
            'uid' => session(C('USER_AUTH_KEY')),
            'fileUpload' => $filepath,
            'type' => I('post.major'),//专业
            'batch' => session('batch'),
            'project_code' => I('post.project_code'),
            );
            $logdata = M('logdata');
            if ($logdata->add($data)) {
                $this->success('提交成功', U('Home/Module/baseInfo', array('basetype' => I('post.major'))));
            } else {
                $this->error('提交失败');
            }
        }
    }

    // 上传文件方法
    private function up($filename)
    {
        $upload = new \Think\Upload();
        $upload->maxSize = 20000000;//byte为单位
        $upload->exts = array('jpg','gif','png','jpeg');
        $upload->rootPath = './Upload/';
        $upload->saveName = date('ymdhis').'_'.mt_rand();
        $upload->autoSub = true;
        $upload->subName = date('Y-m-d');
        if ($info = $upload->uploadOne($filename)) {
            return $info['savepath'].$info['savename'];
        } else {
            $this->error($upload->getError());
        }
    }
}
